package servlet.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.bean.UserBean;
import servlet.model.ServiceFactory;
import servlet.publicUtils.MyUtils;

public class UserInfoAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//request.setCharacterEncoding("UTF-8");
		UserBean userB = (UserBean) MyUtils.getNewInstance(UserBean.class);
		userB.setUser_id(MyUtils.obj2Integer(request.getParameter("userid")));
		userB.setUsername(request.getParameter("username"));
		userB.setUser_psw(request.getParameter("password"));
		userB.setUser_age(MyUtils.ObjtoInt(request.getParameter("age")));
		userB.setUser_sex(request.getParameter("sex"));
		userB.setUser_address(request.getParameter("address"));
		userB.setUser_phone(request.getParameter("phone"));
		userB.setUser_role(request.getParameter("role"));
		//新建用户
		int i=ServiceFactory.getUserInfoManagerServiceInstance().saveUserInfo(userB);
		
		if(i>0){
			List<UserBean> lstUser=ServiceFactory.getUserInfoManagerServiceInstance().getAllUsersInfo();
			request.getSession().setAttribute("lstUser", lstUser);
			request.removeAttribute("userB");
			
			request.getRequestDispatcher("logined/index.jsp").forward(request, response);
		}else{
			request.getSession().setAttribute("pageErrorInfo", "用户信息添加失败");
			response.sendRedirect("logined/error.jsp");
		}
		
		
	}

}
